package com.yixun.service.impl;

import com.yixun.entity.YxNewSurveyCanRecord;
import com.yixun.entity.YxSurveyXqRecord;
import com.yixun.entity.YxUser;
import com.yixun.mapper.UserMapper;
import com.yixun.mapper.YxSurveyXqRecordMapper;
import com.yixun.service.YxSurveyXqRecordService;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * 畜禽普查记录(YxSurveyXqRecord)表服务实现类
 *
 * @author lbx
 * @since 2021-06-22 11:42:19
 */
@Service("yxSurveyXqRecordService")
public class YxSurveyXqRecordServiceImpl implements YxSurveyXqRecordService {
    @Resource
    private YxSurveyXqRecordMapper yxSurveyXqRecordMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public YxSurveyXqRecord queryById(Long id) {
        return this.yxSurveyXqRecordMapper.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<YxSurveyXqRecord> queryAllByLimit(int offset, int limit) {
        return this.yxSurveyXqRecordMapper.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param yxSurveyXqRecord 实例对象
     * @return 实例对象
     */
    @Override
    public int insert(HashMap<String,Object> yxSurveyXqRecord) {
        return this.yxSurveyXqRecordMapper.insert(yxSurveyXqRecord);
    }

    /**
     * 修改数据
     *
     * @param yxSurveyXqRecord 实例对象
     * @return 实例对象
     */
    @Override
    public YxSurveyXqRecord update(YxSurveyXqRecord yxSurveyXqRecord) {
        this.yxSurveyXqRecordMapper.update(yxSurveyXqRecord);
        return this.queryById(yxSurveyXqRecord.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.yxSurveyXqRecordMapper.deleteById(id) > 0;
    }

    @Override
    public Integer updateEnvironment(HashMap<String, Object> map) {
         return this.yxSurveyXqRecordMapper.updateEnvironment(map);
    }

    @Override
    public int updateStatus(Integer id) {
        YxSurveyXqRecord record = this.queryById(Long.valueOf(id));
        HashMap<String, Integer> map = new HashMap<>();
        map.put("id",id);
        String status = record.getStatus();
        switch (status){
            case "0":{
                map.put("status",10);
                break;
            }
            case "1":
            case "10": {
                map.put("status",20);
                break;
            }
            case "20":{
                map.put("status",30);
                break;
            }
            case "30":{
                map.put("status",31);
                break;
            }
            case "31":{
                break;
            }
        }
        if (!status.equals("31")) {
            return this.yxSurveyXqRecordMapper.updateStatus(map);
        }
        return 0;
    }
    
    @Resource
    private UserMapper userMapper;
    @Override
    public int goBack(Integer id) {
        YxSurveyXqRecord record = this.queryById(Long.valueOf(id));
        String userId = record.getAddUser();
        HashMap<String, Integer> map = new HashMap<>();
        map.put("id",id);
        YxUser userBean = userMapper.getUserBean(userId);
        Integer user_type = userBean.getUser_type();
        if (user_type==1){
            map.put("status",0);
        }
        if (user_type==2){
            map.put("status",1);
        }
        return this.yxSurveyXqRecordMapper.goBack(map);
    }

    @Override
    public List<YxSurveyXqRecord> queryAll(HashMap<String, Object> map) {
        return yxSurveyXqRecordMapper.searchAll(map);

    }

    @Override
    public int getCount() {
        return yxSurveyXqRecordMapper.getCount();
    }

    @Override
    public YxSurveyXqRecord getOne(Long id) {
        return yxSurveyXqRecordMapper.getOne(id);
    }

    @Override
    public int updateOne(HashMap<String, Object> map) {
        return yxSurveyXqRecordMapper.updateOne(map);
    }

    @Override
    public HSSFWorkbook export() {
        List<YxSurveyXqRecord> list = yxSurveyXqRecordMapper.queryAll(null);
        String[] excelHeader = { "序号","数据编号","行政村","类型",
                "品种名称", "群体数量","能繁母畜数量","种公畜数量","普查时间","普查人员","普查阶段"};
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("已知畜禽普查");
        HSSFRow row = sheet.createRow((int) 0);
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        for (int i = 0; i < excelHeader.length; i++) {
            HSSFCell cell = row.createCell(i);
            cell.setCellValue(excelHeader[i]);
            cell.setCellStyle(style);
            sheet.autoSizeColumn(i);
            sheet.setColumnWidth(i, 252*15+323);
        }
        sheet.setColumnWidth(8, 252*15+623);
        for (int i = 0; i < list.size(); i++) {
            row = sheet.createRow(i + 1);
            row.setHeight((short)400);
            YxSurveyXqRecord surveyXqRecord = list.get(i);
            row.createCell(0).setCellValue(i+1);
            row.createCell(1).setCellValue(surveyXqRecord.getId());
            row.createCell(2).setCellValue(surveyXqRecord.getVillageId().getDqName());
            row.createCell(3).setCellValue(surveyXqRecord.getSourceType().equals("1")?"已有品种":"新发现品种");
            row.createCell(4).setCellValue(surveyXqRecord.getExistVarietyId());
            row.createCell(5).setCellValue(surveyXqRecord.getAllNum());
            row.createCell(6).setCellValue(surveyXqRecord.getFemaleNum());
            row.createCell(7).setCellValue(surveyXqRecord.getMaleNum());
            row.createCell(8).setCellValue(surveyXqRecord.getPcDay());
            row.createCell(9).setCellValue(surveyXqRecord.getUserId());
            row.createCell(10).setCellValue(surveyXqRecord.getStatus());
        }
        return wb;
    }

    @Override
    public int getSearchCount (HashMap<String, Object> map) {
        return yxSurveyXqRecordMapper.getSearchCount(map);
    }
}
